package com.android.delivery.activity.fragment

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import com.android.delivery.adapter.PrintCloudAdapter
import com.android.delivery.api.ApiService
import com.android.delivery.base.BaseListFragmet
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.entity.CloudPrinter
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.include_refresh_layout.*

/**
 * 打印机-云打印
 */
class PrintlnCloudListFragment : BaseListFragmet<CloudPrinter>() {

    val TRANSFER_REQUESTCODE = 1;

    private val printlnAdapter: PrintCloudAdapter by lazy {
        PrintCloudAdapter()
    }

    companion object {
        fun newInstance(): PrintlnCloudListFragment {
            val fragment = PrintlnCloudListFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun bindRecyclerView(recLv: RecyclerView?) {
        recLv!!.adapter = printlnAdapter
        printlnAdapter.setOnItemClickListener(object : PrintCloudAdapter.OnItemClickListener {
            override fun connDevice(device: CloudPrinter) {
                ApiService.printerConn(this, device.deviceId, object : DialogCallback<LzyResponse<Any>>() {
                    override fun onSuccess(response: Response<LzyResponse<Any>>?) {
                        onRefresh(refreshLayout)
                    }
                })
            }

        })
    }

    override fun loadData(page: Int, pageCount: Int) {
        ApiService.getPrintCloudList(this, page, object : DialogCallback<LzyResponse<CloudPrinter>>() {
            override fun onSuccess(response: Response<LzyResponse<CloudPrinter>>?) {
                onGetDataSuc(response!!.body().data.records)
            }
        })
    }

    override fun getData(): MutableList<CloudPrinter> {
        return printlnAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<CloudPrinter>?) {
        if (isRefresh) {
            printlnAdapter.setNewData(list)
        } else {
            printlnAdapter.addData(list!!)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == TRANSFER_REQUESTCODE) {
            if (resultCode == Activity.RESULT_OK) {
                onRefresh(refreshLayout)
            }
        }
    }
}